package com.starlz.jpa.service.impl;

import com.starlz.jpa.dao.UserDao;
import com.starlz.jpa.entity.LocalUser;
import com.starlz.jpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: wangzi
 * @Date: 2018/11/20 09:55
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Override
    public Page<LocalUser> findAll(){

        Page<LocalUser> page = userDao.findAll(PageRequest.of(0, 10, new Sort(Sort.Direction.DESC, "username")));

        return page;
    }

    @Override
    public LocalUser sava(LocalUser user) {

        String password = new BCryptPasswordEncoder().encode(user.getPassword());
        user.setPassword(password);
        user = userDao.save(user);
        return user;
    }
}
